/**
 * User: Obi
 * Date: 02.08.11
 * Time: 21:42
 */
package com.jediflasher.game_client.view.world {
	import com.greensock.TweenNano;
	import com.jediflasher.game_client.data.world.CharacterData;
	import com.jediflasher.game_client.data.world.WorldData;
	import com.jediflasher.game_client.events.model.CharacterDataEvent;
	import com.jediflasher.game_client.events.model.WorldDataEvent;
	import com.jediflasher.game_client.events.view.WorldViewEvent;
	import com.jediflasher.game_client.view.*;

	import flash.display.Graphics;
	import flash.display3D.textures.CubeTexture;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	public class WorldView extends BaseView {

		public function WorldView(data:WorldData) {
			super(data);
			this._worldData = data;
		}

		/**
		 * @private
		 */
		private var _worldData:WorldData;

		/**
		 * id – CharacterView
		 */
		private var _charactersHash:Object = new Object();

		override public function render(event:Event = null):Boolean {
			if (!super.render(event)) return false;

			var g:Graphics = super.graphics;
			g.clear();
			g.beginFill(0xCCDDFF);
			g.drawRect(0, 0, super.stage.stageWidth, super.stage.stageHeight);
			g.endFill();

			this.addEventListener(MouseEvent.CLICK, this.handler_click);

			this._worldData.addEventListener(WorldDataEvent.CHARACTER_ADDED, this.handler_characterAdded);

			var currentCharacters:Vector.<CharacterData> = this._worldData.charactersList;

			var len:int = currentCharacters.length;
			for (var i:int = 0; i < len; i++) {
				var charData:CharacterData = currentCharacters[i];
				var charId:int = charData.id;

				var view:CharacterView = this._charactersHash[charId];
				if (!view) {
					this._charactersHash[charId] = view = new CharacterView(charData);
					super.addChild(view);
					
					if(charData.x) view.x = charData.x;
					if(charData.y) view.y = charData.y;
				}
			}

			return true;
		}


		override public function clear(event:Event = null):void {
			super.clear(event);

			for (var charId:String in this._charactersHash) {
				var charView:CharacterView = this._charactersHash[charId] as CharacterView;
				super.removeChild(charView);

				delete this._charactersHash[charId];

				var charData:CharacterData = this._worldData.getCharacterById(parseInt(charId));
				charData.removeEventListener(CharacterDataEvent.POSITION_CHANGE, this.handler_positionChange);
			}

			this._worldData.removeEventListener(WorldDataEvent.CHARACTER_ADDED, this.handler_characterAdded);
		}

		/**
		 * @private
		 */
		private function moveCharacter(id:int, x:Number, y:Number):void {
			var charView:CharacterView = this._charactersHash[id] as CharacterView;

			TweenNano.to(charView, 5, {x:x, y:y});
		}

		/**
		 * @private
		 */
		private function handler_characterAdded(event:WorldDataEvent):void {
			var characterData:CharacterData = event.data as CharacterData;
			characterData.addEventListener(CharacterDataEvent.POSITION_CHANGE, this.handler_positionChange);
			var character:CharacterView = new CharacterView(event.data as CharacterData);

			if (characterData.x) character.x = characterData.x;
			if (characterData.y) character.y = characterData.y;

			this._charactersHash[characterData.id] = character;

			super.addChild(character);
		}

		/**
		 * private
		 */
		private function handler_positionChange(event:CharacterDataEvent):void {
			var charData:CharacterData = event.target as CharacterData;
			var charId:int = charData.id;

			this.moveCharacter(charId, charData.x, charData.y);
		}

		/**
		 * @private
		 */
		private function handler_click(event:MouseEvent):void {
			super.dispatchEvent(new WorldViewEvent(WorldViewEvent.CHARACTER_MOVE, new Point(this.mouseX, this.mouseY)));
		}
	}
}
